/*
 * Copyright (c) 2009 Robert Esser
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.e2ser.component;

import net.e2ser.petri.Color;
import net.e2ser.petri.Editor;
import net.e2ser.petri.RobPoint;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;

/**
 *
 * <p>Title: Petri Net Editor/Simulator</p>
 * <p>Description: Represents a Petri Net arc</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * @author Rob Esser
 * @version 1.0
 */
public class Arc extends OneD {

  private static final int DefaultHeadLength = 5;
  private static final int DefaultHeadWidth = 2;
  
  private int headLength = DefaultHeadLength;
  private int headWidth = DefaultHeadWidth;
  private float textSize = 9;
  
  public Arc(AbstractComponent from, AbstractComponent to) {
    super(from, to);
    shape = new ShapeDrawable(new RectShape()); 
    Paint paint = shape.getPaint();
    paint.setColor(Color.BLACK);
    paint.setTextAlign(Align.CENTER);
    recalulateTextSize();
    paint.setTypeface(Typeface.SANS_SERIF);
  }

  public void recalulateTextSize() {
    textSize = to.textSize();
    shape.getPaint().setTextSize(textSize);
  }
  
  public void resize(double scale) {
    headLength = (int) (DefaultHeadLength * scale);
    headWidth = (int) (DefaultHeadWidth * scale);
    recalulateTextSize();
  }

  /**
   * draw myself on the Canvas
   * @param canvas where to draw
   */
  public void draw(Canvas canvas) {
    Rect r = new Rect(to.shape.getBounds());
    r.union(from.shape.getBounds());
    shape.setBounds(r);

    RobPoint one, two;
    one = from.connectionPoint(to.center());
    two = to.connectionPoint(from.center());
    // the arrow head and line
    RobPoint vect = two.sub(one);
    double len = vect.radius();
    if (len != 0.0) {
      RobPoint modTwo = two.sub(vect.unitVector().mult(headLength));
      RobPoint aPt = modTwo.add(vect.normal().mult(headWidth));
      canvas.drawLine((float) two.x, (float) two.y, (float) aPt.x, (float) aPt.y, shape.getPaint());
      RobPoint aPt2 = modTwo.sub(vect.normal().mult(headWidth));
      canvas.drawLine((float) two.x, (float) two.y, (float) aPt2.x, (float) aPt2.y, shape.getPaint());
      canvas.drawLine((float) aPt.x, (float) aPt.y, (float) aPt2.x, (float) aPt2.y, shape.getPaint());
      two = modTwo;
    }
    //the arc itself
    canvas.drawLine((float) one.x, (float) one.y, (float) two.x, (float) two.y, shape.getPaint());

    // now the inscription text
    String s = "";
    if (s.length() > 0) {
      shape.getPaint().setColor(Color.GRAY);
      canvas.drawText(s, r.centerX() + Editor.TextOffsetX, r.centerY() + Editor.TextOffsetX, shape.getPaint());
    }
  }

}
